package com.cl.unipay4j.wx.pay;

import com.cl.unipay4j.core.IPayConfig;
import com.cl.unipay4j.core.pay.PayModel;
import com.cl.unipay4j.core.Result;
import com.cl.unipay4j.wx.sdk.WXPayUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WxPayNative
 * @Description 微信公众号支付
 * @Author Small
 * @Date 2019/11/28 9:52
 * @Version 1.0
 **/
@Slf4j
public class WxPayJSAPI extends WxPay {

    public WxPayJSAPI() {
        super();
    }

    public WxPayJSAPI(IPayConfig payConfig) {
        super(payConfig);
    }

    /**
     * 方法描述: 支付
     * @Author Small
     * @param model
     * @Date 2019/11/29 10:02
     * @return com.weixing.mall.pay.PayResult
     */
    @Override
    public Result pay(PayModel model) {
        return doPay(model, WxTradeTypeEnum.JSAPI);
    }


    /**
     * 是否需要openId参数
     * @return
     */
    @Override
    public boolean needOpenId() {
        return true;
    }


    @Override
    public Result doTradeSuccess(Map<String, String> r) throws Exception {
        Map<String, String> map = new HashMap<>(8);
        map.put("appId", r.get("appid"));
        map.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
        map.put("nonceStr", WXPayUtil.generateNonceStr());
        map.put("signType", "MD5");
        map.put("package", "prepay_id=" + r.get("prepay_id"));
        map.put("paySign", WXPayUtil.generateSignature(map, wxPayConfig.getKey(), wxPayConfig.getContext().getSignType()));
        return Result.tradeSuccess(map);
    }
}
